package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public int save(StorePO po) {
        int insert = storeMapper.insert(po);
        return insert;
    }

    @Override
    public PageInfo<StorePO> pagesList(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<StorePO> storePOQueryWrapper = assembleQueryWrapper(query);
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> storePOS = storeMapper.selectList(storePOQueryWrapper);
        return new PageInfo<>(storePOS);
    }


    private QueryWrapper<StorePO> assembleQueryWrapper(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        // 1. 门店名称模糊查询
        if (StringUtils.hasText(query.getStoreName())) {
            queryWrapper.like("store_name", query.getStoreName());
        }
        // 2. 负责人电话精确查询
        if (StringUtils.hasText(query.getStoreHeadPhone())) {
            queryWrapper.eq("store_head_phone", query.getStoreHeadPhone());
        }
        // 3. 省份 ID 精确查询
        if (query.getProvinceId() != null) {
            queryWrapper.eq("province_id", query.getProvinceId());
        }
        // 4. 区域 ID 精确查询
        if (query.getAreaId() != null) {
            queryWrapper.eq("area_id", query.getAreaId());
        }
        // 5. 商家 ID 精确查询
        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }
        // 6. 市区 ID 精确查询
        if (query.getCityId() != null) {
            queryWrapper.eq("city_id", query.getCityId());
        }
        // 7. 更新的时间区间的开始时间精确查询
        if (query.getStartingTime() != null) {
            queryWrapper.ge("starting_time", query.getStartingTime());
        }
        // 8. 更新的时间区间的结束时间精确查询
        if (query.getEndTime() != null) {
            queryWrapper.le("end_time", query.getEndTime());
        }
        // 9. 门店状态精确查询
        if (query.getStoreStatus() != null) {
            queryWrapper.eq("store_status", query.getStoreStatus());
        }
        return queryWrapper;
    }
    @Override
    public List<StorePO> list(QueryWrapper<StorePO> queryWrapper) {
        return storeMapper.selectList(queryWrapper);
    }
}
